{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# bar_chart_race_cn库\n",
    "使用bar_chart_race_cn绘制bar chart races图, \n",
    "\n",
    "### 官方文档\n",
    "bar_chart_race_cn是对bar_chart_race库的汉化版，具体文档可查看原创作者的文档\n",
    "\n",
    "https://www.dexplo.org/bar_chart_race\n",
    "\n",
    "\n",
    "### 安装\n",
    "```\n",
    "pip install bar_chart_race_cn\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 快速上手\n",
    "### 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import bar_chart_race_cn as bcr\n",
    "\n",
    "bcr.bar_chart_race(df=df,\n",
    "                   filename='近20年各省财政收入.mp4',\n",
    "                   title='近20年各省财政收入(单位: 亿元)'\n",
    "                  )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据准备\n",
    "数据自国家统计局，以**财政收入.csv**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京市</th>\n",
       "      <th>天津市</th>\n",
       "      <th>河北省</th>\n",
       "      <th>山西省</th>\n",
       "      <th>内蒙古自治区</th>\n",
       "      <th>辽宁省</th>\n",
       "      <th>吉林省</th>\n",
       "      <th>黑龙江省</th>\n",
       "      <th>上海市</th>\n",
       "      <th>江苏省</th>\n",
       "      <th>...</th>\n",
       "      <th>重庆市</th>\n",
       "      <th>四川省</th>\n",
       "      <th>贵州省</th>\n",
       "      <th>云南省</th>\n",
       "      <th>西藏自治区</th>\n",
       "      <th>陕西省</th>\n",
       "      <th>甘肃省</th>\n",
       "      <th>青海省</th>\n",
       "      <th>宁夏回族自治区</th>\n",
       "      <th>新疆维吾尔自治区</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01</th>\n",
       "      <td>345.00</td>\n",
       "      <td>133.61</td>\n",
       "      <td>248.76</td>\n",
       "      <td>114.48</td>\n",
       "      <td>95.03</td>\n",
       "      <td>295.63</td>\n",
       "      <td>103.83</td>\n",
       "      <td>185.34</td>\n",
       "      <td>485.38</td>\n",
       "      <td>448.31</td>\n",
       "      <td>...</td>\n",
       "      <td>87.24</td>\n",
       "      <td>233.86</td>\n",
       "      <td>85.23</td>\n",
       "      <td>180.75</td>\n",
       "      <td>5.38</td>\n",
       "      <td>114.97</td>\n",
       "      <td>61.28</td>\n",
       "      <td>16.58</td>\n",
       "      <td>20.82</td>\n",
       "      <td>79.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-01-01</th>\n",
       "      <td>454.17</td>\n",
       "      <td>163.64</td>\n",
       "      <td>283.50</td>\n",
       "      <td>132.76</td>\n",
       "      <td>99.43</td>\n",
       "      <td>370.44</td>\n",
       "      <td>121.10</td>\n",
       "      <td>213.64</td>\n",
       "      <td>609.47</td>\n",
       "      <td>572.15</td>\n",
       "      <td>...</td>\n",
       "      <td>106.12</td>\n",
       "      <td>271.12</td>\n",
       "      <td>99.75</td>\n",
       "      <td>191.28</td>\n",
       "      <td>6.11</td>\n",
       "      <td>135.81</td>\n",
       "      <td>69.95</td>\n",
       "      <td>19.82</td>\n",
       "      <td>27.57</td>\n",
       "      <td>95.09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               北京市     天津市     河北省     山西省  内蒙古自治区     辽宁省     吉林省    黑龙江省  \\\n",
       "year                                                                         \n",
       "2000-01-01  345.00  133.61  248.76  114.48   95.03  295.63  103.83  185.34   \n",
       "2001-01-01  454.17  163.64  283.50  132.76   99.43  370.44  121.10  213.64   \n",
       "\n",
       "               上海市     江苏省  ...     重庆市     四川省    贵州省     云南省  西藏自治区     陕西省  \\\n",
       "year                        ...                                                 \n",
       "2000-01-01  485.38  448.31  ...   87.24  233.86  85.23  180.75   5.38  114.97   \n",
       "2001-01-01  609.47  572.15  ...  106.12  271.12  99.75  191.28   6.11  135.81   \n",
       "\n",
       "              甘肃省    青海省  宁夏回族自治区  新疆维吾尔自治区  \n",
       "year                                         \n",
       "2000-01-01  61.28  16.58    20.82     79.07  \n",
       "2001-01-01  69.95  19.82    27.57     95.09  \n",
       "\n",
       "[2 rows x 31 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('data/财政收入.csv')\n",
    "\n",
    "def year2timestamp(year):\n",
    "    #转为可比较大小的日期格式\n",
    "    return pd.Timestamp(str(year))\n",
    "\n",
    "#把year转化为可比较日期大小的行索引index\n",
    "df['year'] = df['year'].apply(year2timestamp)\n",
    "df.set_index('year', inplace=True)\n",
    "df.sort_index(inplace=True)\n",
    "df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import bar_chart_race_cn as bcr\n",
    "\n",
    "bcr.bar_chart_race(df=df,\n",
    "                   filename='近20年各省财政收入.mp4',\n",
    "                   title='近20年各省财政收入(单位: 亿元)'\n",
    "                  )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
